package org.yn.dept.controller;

import org.yn.base.constants.BaseConstants;
import org.yn.dept.dto.ShopExamineDTO;
import org.yn.dept.dto.ShopRegisterDTO;
import org.yn.dept.service.IShopService;
import org.yn.dept.domain.Shop;
import org.yn.dept.query.ShopQuery;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.exception.ResponseCode;

import javax.validation.Valid;
import java.util.List;

@PermissionAnnotation(name="shop管理",descs = "shop管理")
@Api("shop管理接口")
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name="保存和修改Shop",descs = "保存和修改Shop")
    @ApiOperation(value = "保存和修改")
    @PostMapping
    public AjaxResult updateOrInsert(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.insert(shop);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PermissionAnnotation(name="删除Shop",descs = "删除Shop")
    @ApiOperation(value = "删除")
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @PermissionAnnotation(name="获取单个Shop",descs = "获取单个Shop")
    @ApiOperation(value = "获取单个Shop")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id)
    {
        try {
            Shop shop = shopService.selectById(id);
            return AjaxResult.success(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的XX信息
    * @return
    */
    @PermissionAnnotation(name="获取所有Shop",descs = "获取所有Shop")
    @ApiOperation(value = "获取所有Shop")
    @GetMapping
    public AjaxResult selectAll(){

        try {
            List< Shop> list = shopService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageInfo 分页对象
    */
    @PermissionAnnotation(name="分页查询Shop",descs = "分页查询Shop")
    @ApiOperation(value = "分页查询Shop")
    @PutMapping
    public AjaxResult queryPage(@RequestBody ShopQuery query)
    {
        try {
            PageInfo<Shop> pageInfo = shopService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PermissionAnnotation(name="批量删除Shop",descs = "批量删除Shop")
    @ApiOperation(value = "批量删除Shop")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
                shopService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败"+e.getMessage());
        }
    }

    /**
     * 参数接收, employee里面没有confirmPassword
     * 方式一:  在Employee里面加一个confirmPassword字段就可以了
     * 方式二: 单独写一个dto   采纳这种
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@Valid @RequestBody ShopRegisterDTO dto){

        shopService.settlement(dto);
        return AjaxResult.success("店铺入驻成功!");
    }


    @GetMapping("/activation/{id}")
    public AjaxResult activation(@PathVariable("id") Long id){
        try {
            shopService.activationById(id);
            return AjaxResult.success("激活成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("激活失败!"+e.getMessage(), ResponseCode.RESPONSE_CODE_412.getCode());
        }
    }


    @GetMapping("/shopRegister/{id}")
    public AjaxResult shopRegister(@PathVariable("id") Long id){
        try {
           Shop shop= shopService.getShopRegister(id);
            return AjaxResult.success(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("重新编辑失败!"+e.getMessage());
        }
    }

    /**
     *
     * @return
     */
    @PostMapping("/examine")
    public AjaxResult examine(@Valid @RequestBody ShopExamineDTO dto){

        shopService.examine(dto);
        return AjaxResult.success("审核信息修改成功！");
    }
}
